﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Configuration;

namespace com.sinomobile.data.dbutility
{
    public abstract class DbHelperAccess
    {
        private static string sqlerrorlogdir = ConfigurationManager.AppSettings["logDir"];

        protected DbHelperAccess()
        {
        }

        public static int ExecuteNonQuery(string connString, string cmdText, params OleDbParameter[] cmdParms)
        {
            int num2;
            OleDbCommand cmd = new OleDbCommand();
            try
            {
                using (OleDbConnection connection = new OleDbConnection(connString))
                {
                    PrepareCommand(cmd, connection, null, CommandType.Text, cmdText, cmdParms);
                    int num = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    num2 = num;
                }
            }
            catch (Exception exception)
            {
                LogError(exception, cmdText);
                throw exception;
            }
            return num2;
        }

        public static int ExecuteNonQuery(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
        {
            int num2;
            OleDbCommand cmd = new OleDbCommand();
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                int num = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                num2 = num;
            }
            catch (Exception exception)
            {
                LogError(exception, cmdText);
                throw exception;
            }
            return num2;
        }

        public static int ExecuteNonQuery(OleDbTransaction trans, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
        {
            int num2;
            OleDbCommand cmd = new OleDbCommand();
            try
            {
                PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
                int num = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                num2 = num;
            }
            catch (Exception exception)
            {
                LogError(exception, cmdText);
                throw exception;
            }
            return num2;
        }

        public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
        {
            int num2;
            OleDbCommand cmd = new OleDbCommand();
            try
            {
                using (OleDbConnection connection = new OleDbConnection(connString))
                {
                    PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
                    int num = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    num2 = num;
                }
            }
            catch (Exception exception)
            {
                LogError(exception, cmdText);
                throw exception;
            }
            return num2;
        }

        public static OleDbDataReader ExecuteReader(string connString, string cmdText, params OleDbParameter[] cmdParms)
        {
            OleDbDataReader reader2;
            OleDbCommand cmd = new OleDbCommand();
            OleDbConnection conn = new OleDbConnection(connString);
            try
            {
                PrepareCommand(cmd, conn, null, CommandType.Text, cmdText, cmdParms);
                reader2 = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exception)
            {
                conn.Close();
                LogError(exception, cmdText);
                throw exception;
            }
            return reader2;
        }

        public static OleDbDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
        {
            OleDbDataReader reader2;
            OleDbCommand cmd = new OleDbCommand();
            OleDbConnection conn = new OleDbConnection(connString);
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                reader2 = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception exception)
            {
                conn.Close();
                LogError(exception, cmdText);
                throw exception;
            }
            return reader2;
        }

        public static object ExecuteScalar(string connString, string cmdText, params OleDbParameter[] cmdParms)
        {
            object obj3;
            OleDbCommand cmd = new OleDbCommand();
            try
            {
                using (OleDbConnection connection = new OleDbConnection(connString))
                {
                    PrepareCommand(cmd, connection, null, CommandType.Text, cmdText, cmdParms);
                    object obj2 = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    obj3 = obj2;
                }
            }
            catch (Exception exception)
            {
                LogError(exception, cmdText);
                throw exception;
            }
            return obj3;
        }

        public static object ExecuteScalar(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
        {
            object obj3;
            OleDbCommand cmd = new OleDbCommand();
            try
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
                object obj2 = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                obj3 = obj2;
            }
            catch (Exception exception)
            {
                LogError(exception, cmdText);
                throw exception;
            }
            return obj3;
        }

        public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms)
        {
            object obj3;
            OleDbCommand cmd = new OleDbCommand();
            try
            {
                using (OleDbConnection connection = new OleDbConnection(connString))
                {
                    PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
                    object obj2 = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    obj3 = obj2;
                }
            }
            catch (Exception exception)
            {
                LogError(exception, cmdText);
                throw exception;
            }
            return obj3;
        }

        private static void LogError(Exception ex, string sqlstr)
        {
            if (string.IsNullOrEmpty(sqlerrorlogdir))
            {
                sqlerrorlogdir = "./sqlerror";
            }
            string fullPath = Path.GetFullPath(sqlerrorlogdir);
            if (!Directory.Exists(sqlerrorlogdir))
            {
                Directory.CreateDirectory(fullPath);
            }
            fullPath = fullPath + @"\SQLErrorLog_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + ".txt";
            try
            {
                StreamWriter writer;
                StringBuilder builder = new StringBuilder();
                builder.AppendLine("-------- Sql Error--------    " + DateTime.Now.ToString());
                builder.AppendLine("--Exception Message-- " + ex.Message);
                builder.AppendLine("sqlstr:" + sqlstr);
                if (!File.Exists(fullPath))
                {
                    writer = File.CreateText(fullPath);
                }
                else
                {
                    writer = File.AppendText(fullPath);
                }
                writer.WriteLine(builder.ToString());
                writer.Close();
            }
            catch
            {
            }
        }

        private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
            {
                cmd.Transaction = trans;
            }
            cmd.CommandType = cmdType;
            if (cmdParms != null)
            {
                foreach (OleDbParameter parameter in cmdParms)
                {
                    cmd.Parameters.Add(parameter);
                }
            }
        }
    }
}

